package com.gopay.bis.khpay.ebank.operation;

import java.math.BigDecimal;

import org.apache.axis.utils.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.ecc.emp.data.KeyedCollection;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;
import com.gopay.common.exception.GopayException;
import com.sdb.payclient.core.PayclientInterfaceUtil;

/**
 * 平安跨行支付单笔查询处理类
 * 
 * @KhPayQueryOrder.java
 * @author zhengxue
 * @2017-8-18 上午10:12:46 www.gopay.com.cn Inc.All rights reserved.
 */
public class KhPayQueryOrder {

    public Object queryOrder(Object object) throws GopayException {
        KhPayMessage message = (KhPayMessage) object;
        try {
            BankLogUtils.printB2CQuerySendLog(message, ToStringBuilder.reflectionToString(message));

            PayclientInterfaceUtil util = new PayclientInterfaceUtil();
            KeyedCollection input = new KeyedCollection("input");
            KeyedCollection output = new KeyedCollection("output");
            input.put("masterId", message.getMasterId());
            input.put("orderId", message.getBankOrderId());

            output = util.execute(input, message.getTransCode());
            String errorCode = (String) output.getDataValue("errorCode");
            String errorMsg = (String) output.getDataValue("errorMsg");

            if (StringUtils.isEmpty(errorCode) && StringUtils.isEmpty(errorMsg)) {
                // 查询成功
                message.setStatus((String) output.getDataValue("status"));
                message.setAmount(new BigDecimal(String.valueOf(output.getDataValue("amount"))));
                message.setBankOrderId((String) output.getDataValue("orderId"));
                message.setOrderDate((String) output.getDataValue("date"));
                message.setCharge(String.valueOf(output.getDataValue("charge")));
                message.setPaydate((String) output.getDataValue("paydate"));
                message.setValidtime(String.valueOf(output.getDataValue("validtime")));
                message.setSettleflg((String) output.getDataValue("settleflg"));// 1已清算，0待清算
                message.setSettletime((String) output.getDataValue("settletime"));
                message.setChargeflg((String) output.getDataValue("chargeflg"));// 1已清算，0待清算
                message.setChargetime((String) output.getDataValue("chargetime"));
            } else {
                // 查询失败
                message.setErrorCode(errorCode);
                message.setErrorMsg(errorMsg);
            }
            BankLogUtils.printB2CQueryBlackLog(message, ToStringBuilder.reflectionToString(message));
        } catch (Exception e) {
            BankLogUtils.printB2CQueryExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6033);
        }

        return message;
    }

}
